package cn.wanda.utils.config

import java.text.SimpleDateFormat
import java.util.Date
import cn.wanda.utils.sql.TmpTableSQL
import org.apache.spark.sql.types._

trait groupJobTrait extends Serializable with TmpTableSQL {
    /**
      * 最终计算表的表结构
      */
    val schema = StructType(List(
        StructField("data_date", DateType, nullable = true), // 1
        StructField("group_id", IntegerType, nullable = true), // 2
        StructField("member_code", StringType, nullable = true), // 3
        StructField("td_180d_value", FloatType, nullable = true), // 4
        StructField("td_90d_value", FloatType, nullable = true), // 5
        StructField("txl_cnt_value", FloatType, nullable = true), // 6
        StructField("bqs_90d_value", FloatType, nullable = true), // 7
        StructField("rule_els_value", FloatType, nullable = true), // 8
        StructField("total_cnt_value", FloatType, nullable = true), // 9
        StructField("earlist_shidong_rate_value", FloatType, nullable = true), // 10
        StructField("shidong_avg_3mon_value", FloatType, nullable = true), // 11
        StructField("lst_shidong_rate2_value", FloatType, nullable = true), // 12
        StructField("shidong_rate_value", FloatType, nullable = true), // 13
        StructField("tran_paid_amt_incre_1mth_value", FloatType, nullable = true), // 14
        StructField("tran_paid_amt_prt_12mth_value", FloatType, nullable = true), // 15
        StructField("lst_od_dob3_value", FloatType, nullable = true), // 16
        StructField("lst_txn_dob_value", FloatType, nullable = true), // 17
        StructField("od_intvl_max_value", FloatType, nullable = true), // 18
        StructField("od_tran_cnt_1mth_value", FloatType, nullable = true), // 19
        StructField("od_avg_day_12mth_value", FloatType, nullable = true), // 20
        StructField("od_paid_amt_incre_trend_value", FloatType, nullable = true), // 21
        StructField("od_tran_amt_crt_1mth_value", FloatType, nullable = true), // 22
        StructField("max_due_value", FloatType, nullable = true), // 23
        StructField("od_trade_cnt_3mth_value", FloatType, nullable = true), // 24
        StructField("od_paid_ratio_12mth_value", FloatType, nullable = true), // 25
        StructField("od_paid_amt_prt_12mth_value", FloatType, nullable = true), // 26
        StructField("td_180d_goal", FloatType, nullable = true), // 27
        StructField("td_90d_goal", FloatType, nullable = true), // 28
        StructField("txl_cnt_goal", FloatType, nullable = true), // 29
        StructField("bqs_90d_goal", FloatType, nullable = true), // 30
        StructField("rule_els_goal", FloatType, nullable = true), // 31
        StructField("total_cnt_goal", FloatType, nullable = true), // 32
        StructField("earlist_shidong_rate_goal", FloatType, nullable = true), // 33
        StructField("shidong_avg_3mon_goal", FloatType, nullable = true), // 34
        StructField("lst_shidong_rate2_goal", FloatType, nullable = true), // 35
        StructField("shidong_rate_goal", FloatType, nullable = true), // 36
        StructField("tran_paid_amt_incre_1mth_goal", FloatType, nullable = true), // 37
        StructField("tran_paid_amt_prt_12mth_goal", FloatType, nullable = true), // 38
        StructField("lst_od_dob3_goal", FloatType, nullable = true), // 39
        StructField("lst_txn_dob_goal", FloatType, nullable = true), // 40
        StructField("od_intvl_max_goal", FloatType, nullable = true), // 41
        StructField("od_tran_cnt_1mth_goal", FloatType, nullable = true), // 42
        StructField("od_avg_day_12mth_goal", FloatType, nullable = true), // 43
        StructField("od_paid_amt_incre_trend_goal", FloatType, nullable = true), // 44
        StructField("od_tran_amt_crt_1mth_goal", FloatType, nullable = true), // 45
        StructField("max_due_goal", FloatType, nullable = true), // 46
        StructField("od_trade_cnt_3mth_goal", FloatType, nullable = true), // 47
        StructField("od_paid_ratio_12mth_goal", FloatType, nullable = true), // 48
        StructField("od_paid_amt_prt_12mth_goal", FloatType, nullable = true), // 49
        StructField("model_of_the_total_score", FloatType, nullable = true) // 50
    ))

    val dateFormatByDay = new SimpleDateFormat("yyyy-MM-dd")

    val odIntvlMaxSchema = StructType(List(
        StructField("member_code", StringType, nullable = true),
        StructField("od_intvl_max", IntegerType, nullable = true)
    ))

    val shidongSchema = StructType(List(
        StructField("member_code", StringType, nullable = true),
        StructField("shidong_avg_3mon", DoubleType, nullable = true)
    ))

    def NowDate(): String = {
        val now: Date = new Date()
        val dateFormat: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd")
        val date = dateFormat.format(now)
        date
        "2018-09-20"
    }

    def isNull(col: Any): Float = {
        var v = 0f
        if (col == null) {
            v = -99f
        } else {
            v = col.toString.toFloat
        }
        v
    }

    /**
      * 计算天数差的方法
      */
    def diffDay(day1: String, day2: String): Int = {
        ((dateFormatByDay.parse(day1).getTime - dateFormatByDay.parse(day2).getTime) / (1000 * 60 * 60 * 24)).toInt
    }
}
